package com.fimuz.mqmeet.api.boardroom2.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fimuz.mqmeet.api.boardroom2.dao.Boardroom2Dao;
import com.fimuz.mqmeet.api.boardroom2.service.Boardroom2Service;
import com.fimuz.mqmeet.api.boardroom2.service.model.CreateOrUpdateSpModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.CreateOrUpdateSrModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.DeleteSpModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.DeleteSrModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.GetSpModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.GetSrModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.ListSpModel;
import com.fimuz.mqmeet.api.boardroom2.service.model.ListSrModel;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzBoardroomMapper;
import com.fimuz.mqmeet.helper.mybatis.model.HxzBoardroom;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.utils.ExtAsserts;

@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class Boardroom2ServiceImpl extends CoBaseService implements
		Boardroom2Service {
	@Autowired 
	private Boardroom2Dao boardroom2Dao;
	@Autowired
	private HxzBoardroomMapper hxzBoardroomMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(boardroom2Dao.list(spModel));
		srModel.setTotalCount(boardroom2Dao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		HxzBoardroom db = hxzBoardroomMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"会议室");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		HxzBoardroom db = hxzBoardroomMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		hxzBoardroomMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getBuildingregionId(),"大楼区域");
		ExtAsserts.notNull(spModel.getRoomName(),"会议室名称");
		ExtAsserts.notNull(spModel.getMaxPerson(),"容纳人数");
		ExtAsserts.notNull(spModel.getAllowAnonybook(),"是否允许匿名");
		ExtAsserts.notNull(spModel.getOnlyallowHuiwubook(),"是否仅有会务预订");
		ExtAsserts.notNull(spModel.getMemo(),"备注");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		if(StringUtils.isBlank(spModel.getId())){
			HxzBoardroom db = new HxzBoardroom();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			db.setId(getUuid());
			hxzBoardroomMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			HxzBoardroom db = hxzBoardroomMapper.selectByPrimaryKey(spModel.getId());
			db.setBuildingregionId(spModel.getBuildingregionId());
			db.setRoomName(spModel.getRoomName());
			db.setMaxPerson(spModel.getMaxPerson());
			db.setAllowAnonybook(spModel.getAllowAnonybook());
			db.setOnlyallowHuiwubook(spModel.getOnlyallowHuiwubook());
			db.setMemo(spModel.getMemo());
			hxzBoardroomMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
